package myservlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import mybean.data.login;

public class changePassword extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		try {//加载驱动
			Class.forName("com.mysql.jdbc.Driver");	
		}
		catch(Exception e) {}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		login loginBean =null; 
		HttpSession session= req.getSession(true);
		//获取用户更改后的参数  
		String oldpwd = req.getParameter("oldpwd");//旧密码  
        String newpwd = req.getParameter("newpwd");//新密码  
        String confirm = req.getParameter("confirm");//确认新密码 
        if(oldpwd==""||newpwd==""||confirm=="") {
			out.println("<script language = javascript>alert('请完善信息!');");
			out.println("location.href='changePassword.jsp'</script>");
			return;
		}
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean a=loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(a){
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>");
				return;
			}	
			String pwd = loginBean.getPassword();//得到该用户当前的密码 	
			String role=loginBean.getrole();  //用户身份		
	        if(oldpwd.equals(pwd)){ //输入的旧密码与原密码一致  
	        	if(newpwd.equals(confirm)){//判断输入的两个新密码是否一致  
	        		if(!(newpwd.equals(pwd))){//如果新密码与原密码不同，执行更新密码操作   
	                    //修改数据库中用户的密码                                                                                                                       
	            		String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8";
	            		Connection con = null;
	            		PreparedStatement sql = null;
	            		String condition="";
	                    if(role.equals("用户"))//用户就修改用户表
	                    	condition="update user set Upassword='"+newpwd+"' where Nickname='"+loginBean.getLogname()+"'";
	                    else//管理员修改管理员表
	                    	condition="update administrator set Apassword='"+newpwd+"' where Aname='"+loginBean.getLogname()+"'";
	                    System.out.println(loginBean.getrole());
	                    try {
	            			// 1. 建立与数据库的连接 
	            			con = DriverManager.getConnection(url,"root","744615");
	            			sql=con.prepareStatement(condition);
	            			sql.executeUpdate();                  
	            			sql.close();
	            			//修改 loginBean中的信息 
	            			loginBean.setPassword(newpwd); 
	            			//保存到request中，转发显示  
	            			req.setAttribute("loginBean",loginBean); 
	            			out.println("<script language = javascript>alert('修改成功!');");
	            			out.println("location.href='changePassword.jsp'</script>"); 	        
	            		}
	            		catch (SQLException exp){
	            			out.println("<script language = javascript>alert('更新数据库失败，修改失败!');");
		            		out.println("location.href='changePassword.jsp'</script>"); 
	            			System.out.println(exp);
	            		}
	            	    con.close();   
	                 }
	                else if(newpwd.equals(pwd)){  	                   
	                    out.println("<script language = javascript>alert('密码没有改动!');");
		            	out.println("location.href='changePassword.jsp'</script>"); 	                
	                }  
	            }
	            else{
	        	   out.println("<script language = javascript>alert('两次密码输入不一致!');");
	               out.println("location.href='changePassword.jsp'</script>");                   
	            }  
	        }
	        else{
	    	    out.println("<script language = javascript>alert('旧密码输入错误!');");
                out.println("location.href='changePassword.jsp'</script>");  	            	
	        }  
	    }
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('修改密码失败!');");
            out.println("location.href='changePassword.jsp'</script>");  
		}				
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
